package patrol.mobile.com.mobilepatrol.utils.net;

import android.util.Log;
import android.widget.Toast;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.utils.Constants;

/**
 * Created by K on 2018/9/13.
 * des: 解析带attachList的callback
 */
public abstract class HttpCallBack2 extends StringCallback {
    private static final String TAG = "HttpCallBack";
    private HttpSender sender;

    public HttpCallBack2(HttpSender sender) {
        this.sender = sender;
    }

    @Override
    public void onError(Call call, Exception e, int id) {
        sender.dismissDialog();
        String exceptionMsg = (e == null) ? "" : e.toString();
        Log.i(TAG, "-------------------------------------------------------------------------");
        Log.i(TAG, "Http CallBack onError:");
        try {
            Log.i(TAG,
                    "id = " + id +
                            " isExecuted = " + call.isExecuted() +
                            " exception = " + exceptionMsg);
        } catch (NullPointerException nep) {
            //
        }
        if (sender.getContext() == null) return;
        Toast.makeText(sender.getContext(), "服务器错误", Toast.LENGTH_SHORT).show();
        Log.i(TAG, "-------------------------------------------------------------------------");
        onError(id + "", call, e);
    }

    @Override
    public void onResponse(String response, int id) {
        sender.dismissDialog();
        if (sender.getContext() == null) return;
        //
        Log.i(TAG, "-------------------------------------------------------------------------");
        Log.i(TAG, "Http CallBack onResponse:");
        Log.i(TAG, "id = " + id);
//        Logs.i(TAG, "code = " + code + " string = " + response);
        Log.i(TAG, "response = " + response);
        Log.i(TAG, "-------------------------------------------------------------------------");
        JsonParser jp = new JsonParser();

        // 可能不是一个 JSON 对象
        try {
            JsonObject jsonObject1 = jp.parse(response).getAsJsonObject();
            String code = jsonObject1.get(Constants.HTTP_KEY_CODE).getAsString();
            String errorMsg = jsonObject1.get(Constants.MSG).getAsString();
            if (Constants.SUCCEED.equals(code)) {
                JsonElement element = jsonObject1.get(Constants.HTTP_KEY_DATA);
                JsonElement attachList = jsonObject1.get(Constants.ATTACH_LIST).getAsJsonArray();
                if (element == null) {
                    onResponse(code, "", attachList.toString());
                } else {
                    onResponse(code, element.toString(), attachList.toString());
                }
            } else {
                Toast.makeText(sender.getContext(), errorMsg, Toast.LENGTH_SHORT).show();
                onError(code, null, null);
            }
        } catch (JsonSyntaxException e) {
            Log.w(TAG, "Json syntax exception");
            onError("-1", null, null);//-1 解析错误
        }

    }

    public abstract void onError(String code, Call call, Exception e);

    public abstract void onResponse(String code, String response, String attachList);
}
